﻿using System;
using System.Threading;

namespace Program15
{
    class Program
    {
        static void Main(string[] args)
        {
            ////先暂停执行3秒
            //Thread.Sleep(3000);
            ////Thread:创建和控制一个线程，设置他的优先级，并获取他的状态
            //Console.WriteLine("Hello World!");

            //Program thread1 = new Program();
            //thread1.Method();

            ////创建另一个线程 
            //ThreadStart ts = new ThreadStart(thread1.Method);
            //Thread t = new Thread(ts);
            //t.Start();

            Console.WriteLine("主线程");
            Thread t = new Thread(ThreadProc);
            t.Start();
            //停止线程
            //t.Abort();


            for (var i=0;i<5; i++)
            {
                Console.WriteLine("主线程执行别的任务");
                Thread.Sleep(0);
            }

            Console.WriteLine("主线程:要等待ThreadProc执行完成");
            t.Join();


            //Thread d = new Thread(ThreadProc);
            //t.Start();

            //阻塞调用线程，直到某个线程执行完成

            for (var i = 5; i < 10; i++)
            {
                Console.WriteLine("主线程执行别的任务");
                Thread.Sleep(0);
            }
        }
        

        public void Method()
        {
            int i = 1;
            int j = 2;
            int result = i + j;
            Console.WriteLine("线程{0}，计算结果{1}", AppDomain.GetCurrentThreadId().ToString(), result);
        }


        static void ThreadProc()
        {
            for(var i = 0; i < 10; i++)
            {
                Console.WriteLine("ThreadProc:" + i);
            }
        }
    }
}
